package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *  ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    /**
     * 查看订单
     * @param request
     * @return
     */
    @Override
    public OrderListResponse checkOrder(OrderListRequest request) {
//        Example example = new Example(Order.class);
//        Example.Criteria criteria = example.createCriteria();
        request.requestCheck();
//        if (request.getPage()==null){
//            request.setPage(5);
//        }
        if (request.getSort()==null){
            request.setSort("update_time");
        }
        List<Integer> listStatus = new ArrayList<>();
        listStatus.add(0);
        listStatus.add(1);
        listStatus.add(4);

        OrderListResponse response = new OrderListResponse();

        //int pageNum = (request.getPage()-1)*request.getSize();
        List<OrderDetailInfo> data = new ArrayList<>();
        PageHelper.startPage(request.getPage(),request.getSize());
        List<Order> orders = orderMapper.selectOrder(listStatus,request.getSort(),request.getUserId());
        //List<Order> orders = orderMapper.selectOrder(listStatus,request.getSort(),request.getSize(),pageNum,request.getUserId());
        //List<Order> orders = orderMapper.selectOrder(request.getSort(),request.getSize(),pageNum,request.getUserId());
        if (orders.size() == 0){

            response.setTotal((long)0);

            response.setData(data);
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
            return response;
        }

        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        long total = pageInfo.getTotal();

        //List<OrderDetailInfo> data = new ArrayList<>();
        for (Order order : orders) {
            OrderDetailInfo odi = new OrderDetailInfo();
            odi.setOrderId(order.getOrderId());
            odi.setPayment(order.getPayment());
            odi.setStatus(order.getStatus());
            odi.setCreateTime(order.getCreateTime());
            odi.setUpdateTime(order.getUpdateTime());
            odi.setUserId(order.getUserId());
            odi.setBuyerNick(order.getBuyerNick());

            List<OrderItemDto> orderItemDto = new ArrayList<>();

            Example example = new Example(Order.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("orderId",order.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example);

            for (OrderItem orderItem : orderItems) {
                OrderItemDto orderItemDtos = new OrderItemDto();
                orderItemDtos.setId(orderItem.getId());
                orderItemDtos.setItemId(orderItem.getItemId()+"");
                orderItemDtos.setOrderId(orderItem.getOrderId());
                orderItemDtos.setNum(orderItem.getNum());

                BigDecimal priced= new BigDecimal(orderItem.getPrice());
                orderItemDtos.setPrice(priced);

                BigDecimal totalfee = new BigDecimal(orderItem.getTotalFee());
                orderItemDtos.setTotalFee(totalfee);

                String[] split = orderItem.getPicPath().split(",");
                orderItemDtos.setPicPath(split[0]);
                orderItemDto.add(orderItemDtos);
            }
            odi.setOrderItemDto(orderItemDto);

            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());
            OrderShippingDto orderShippingDto = new OrderShippingDto();
            orderShippingDto.setOrderId(orderShipping.getOrderId());
            orderShippingDto.setReceiverName(orderShipping.getReceiverName());
            orderShippingDto.setReceiverPhone(orderShipping.getReceiverPhone());
            orderShippingDto.setReceiverAddress(orderShipping.getReceiverAddress());

            odi.setOrderShippingDto(orderShippingDto);
            data.add(odi);



        }
        response.setData(data);
        response.setTotal(total);

        response.setCode(OrderRetCode.SUCCESS.getCode());
        response.setMsg(OrderRetCode.SUCCESS.getMessage());
        return response;
    }

    /**
     * 查看订单细节
     * @param
     * @return
     */
    @Override
    public OrderDetailResponseJAL checkDetailOrder(String orderId) {
//        String orderId = orderDetailRequest.getOrderId();

        //查order表
        Order order = orderMapper.selectByPrimaryKey(orderId);
        OrderDetailResponseJAL orderDetailResponseJAL = new OrderDetailResponseJAL();
        orderDetailResponseJAL.setUserId(order.getUserId());
        orderDetailResponseJAL.setUserName(order.getBuyerNick());
        orderDetailResponseJAL.setOrderTotal(order.getPayment());
        orderDetailResponseJAL.setOrderStatus(order.getStatus());

        //查orderShipping表
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
        orderDetailResponseJAL.setTel(orderShipping.getReceiverPhone());
        orderDetailResponseJAL.setStreetName(orderShipping.getReceiverAddress());

        //查orderItem表
        Example example = new Example(OrderItem.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId",order.getOrderId());
        List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
        List<OrderItemDto> newOrderItem = new ArrayList<>();//添加到orderDetailResponseJAL.goodsList
        for (OrderItem orderItem : orderItems) {
            OrderItemDto orderItemDto = new OrderItemDto();
            orderItemDto.setId(orderItem.getId());
            orderItemDto.setItemId(orderItem.getItemId()+"");
            orderItemDto.setOrderId(orderItem.getOrderId());
            orderItemDto.setNum(orderItem.getNum());
            orderItemDto.setTitle(orderItem.getTitle());

            BigDecimal price = new BigDecimal(orderItem.getPrice());
            orderItemDto.setPrice(price);

            BigDecimal totalfee = new BigDecimal(orderItem.getTotalFee());
            orderItemDto.setTotalFee(totalfee);

            orderItemDto.setPicPath(orderItem.getPicPath());
            newOrderItem.add(orderItemDto);
        }
        orderDetailResponseJAL.setGoodsList(newOrderItem);
        orderDetailResponseJAL.setCode(OrderRetCode.SUCCESS.getCode());
        orderDetailResponseJAL.setMsg(OrderRetCode.SUCCESS.getMessage());
        return orderDetailResponseJAL;
    }

    //查询itemId
    @Override
    public List<Long> checkItemByOrderId(String orderId) {

        Example example = new Example(OrderItem.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId",orderId);
        List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
        List<Long> list = new ArrayList<>();
        for (OrderItem orderItem : orderItems) {
            list.add(orderItem.getItemId());
        }
        return list;
    }
}
